<template>
	<view>
		<view class="cu-bar bg-white ">
			<view class="action">
				{{title}}
			</view>
			<view class="action">
				{{imgList.length}}/{{limit}}
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
					:data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<=limit">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		FormatDateTime,
		ReplaceImageHttp,
		GetFileNameByPath
	} from "@/utils/comm.js";
	import store from "@/store";
	import {
		mapGetters
	} from "vuex";
	export default {
		computed: {
			...mapGetters(["userInfo"]),
		},
		name: "uploadImage",
		props: {
			value: {
				type: [Number, String],
				default: ''
			},
			title:{
				type:String,
				default:'图片上传'
			},
			limit:{
				type:Number,
				default:4
			}
		},
		watch: {

			"value": {
				immediate: true, //该回调将会在侦听开始之后被立即调用
				handler: function(n, o) {
					if (n) {
						this.imgList = n.split(",").map(x => {
							return ReplaceImageHttp(x);
						});
					}
				}
			}
		},
		data() {
			return {
				imgList: [],
			};
		},
		methods: {
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '你确定要删除吗？',
					cancelText: '考虑下',
					confirmText: '删除',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							this.CovertUrl();
						}
					}
				})
			},
			async ChooseImage() {
				var {
					tempFilePaths,
					tempFiles
				} = await this.$comm.ChooseImageAsync(1);

				if (tempFilePaths && tempFilePaths.length > 0) {
					let {
						Data
					} = await this.$http.Upload(tempFilePaths[0]);

					for (var item of Data) {
						console.log(item);
						this.imgList.push(item.Url);
					}
					this.CovertUrl();
				}
			},
			async CovertUrl() {
				let url = this.imgList.length > 0 ? this.imgList.join(",") : "";
				this.$emit('input', url);
			}

		}
	}
</script>

<style lang="less">

</style>